package com.egg.ylt.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.CustomUtils;
import com.egg.ylt.Utils.RecyclerViewUtil;
import com.egg.ylt.Utils.StatusBarUtil;
import com.egg.ylt.adapter.ADA_MessageRecyclerAdapter;
import com.egg.ylt.pojo.InformationsEntity;
import com.egg.ylt.presenter.impl.MessagePresenterImpl;
import com.egg.ylt.view.IMessageCenterView;
import com.yonyou.framework.library.base.BaseActivity;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.ListUtil;

import butterknife.BindView;

public class ACT_MessageCenter extends BaseActivity<MessagePresenterImpl> implements IMessageCenterView {

    @BindView(R.id.include_rl_view)
    RelativeLayout mIncludeRlView;
    @BindView(R.id.include_iv_back)
    ImageView includeIvBack;
    @BindView(R.id.include_tv_title)
    TextView includeTvTitle;
    @BindView(R.id.message_recycler_view)
    RecyclerView messageRecyclerView;
    @BindView(R.id.loading_target_view)
    RelativeLayout loadingTargetView;
    @BindView(R.id.message_swipe_refresh)
    SwipeRefreshLayout mSwipeRefresh;

    private String pageSize = "20";
    private int listSize = 0;
    private int pageNum = 1;
    private RecyclerViewUtil mRecyclerViewUtil;
    private int mRefreshStateInt = 0;
    private ADA_MessageRecyclerAdapter mMessageAdapter;

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_message_center;
    }

    @Override
    protected View getLoadingTargetView() {
        return loadingTargetView;
    }

    @Override
    protected void initViewsAndEvents() {

        StatusBarUtil.setStatusBarColor(this, R.color.ylt_whit);
        StatusBarUtil.StatusBarLightMode(this);
        CustomUtils.setImmersiveStatusBar(mContext, mIncludeRlView);

        //设置title
        includeTvTitle.setText("消息");

        includeIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //recyclerview的设置
        initRecycler();

        //设置刷新和加载
        initRefresh();
    }

    private void initRecycler() {
        //布局管理器和适配器
        messageRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mMessageAdapter = new ADA_MessageRecyclerAdapter(mContext);
        messageRecyclerView.setAdapter(mMessageAdapter);

        //请求数据
        mPresenter.getMessageListData(Constants.TOKEN, Constants.COMPANYID, String.valueOf(pageNum), pageSize);
    }

    //设置listview的刷新和加载
    public void initRefresh() {
        //下拉刷新
        mSwipeRefresh.setColorScheme(R.color.color_FE752D);
        mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mRefreshStateInt = 0;
                pageNum = 1;
                mPresenter.getMessageListData(Constants.TOKEN, Constants.COMPANYID, String.valueOf(pageNum), pageSize);
            }
        });

        //设置上拉加载更多
        mRecyclerViewUtil = new RecyclerViewUtil(messageRecyclerView);
        mRecyclerViewUtil.setRecyclerViewLoadMoreListener(new RecyclerViewUtil.RecyclerViewLoadMoreListener() {
            @Override
            public void onLoadMore() {
                if (listSize == Integer.parseInt(pageSize)) {
                    mRefreshStateInt = 1;
                    pageNum++;
                    //请求门店展示数据
                    mPresenter.getMessageListData(Constants.TOKEN, Constants.COMPANYID, String.valueOf(pageNum),
                            pageSize);
                    mRecyclerViewUtil.setLoadMoreEnable(false);
                }
            }
        });
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return false;
    }

    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @Override
    public void getMessageList(InformationsEntity entity) {
        if (ListUtil.isListEmpty(entity.getList())) {
            mSwipeRefresh.setRefreshing(false);
            mRecyclerViewUtil.setLoadMoreEnable(true);
            toggleShowEmpty(true, R.drawable.empty_no_response, "空空如也~");
            return;
        }
        listSize = entity.getList().size();
        toggleShowEmpty(false, -1, "");

        //获取门店数据 0初始化和刷新状态   1 是滑到底部加载状态
        switch (mRefreshStateInt) {
            case 0:
                mMessageAdapter.setDataList(entity.getList());
                //在onRefresh 里调用获取数据的方法就好了 数据获取完毕 别忘了 修改状态
                mSwipeRefresh.setRefreshing(false);
                break;
            case 1:
                mMessageAdapter.addDataList(entity.getList());
                //加载完数据后回复上拉状态
                mRecyclerViewUtil.setLoadMoreEnable(true);
                break;
        }

    }
}
